
#import "Controller.h"

@implementation Controller

- appDidInit:sender
{
	[[NXApp appListener] setServicesDelegate:self];
	return self;
}

- app:sender willShowHelpPanel:panel
{
    char path[MAXPATHLEN + 1];

    sprintf (path, "%s/%s", [panel helpDirectory],
      "GettingStarted.rtfd");
    [panel showFile:path atMarker:NULL];
    return self;
}

- comment:(id)pasteboard
	userData:(const char *)language
	error:(char **)errorMessage
{
	const char *inData;
	char *outData; 
	int   inLength, outLength;
    const char *const *types;
    int hasAscii, i;
    types = [pasteboard types];
    hasAscii=0;
    for (i=0; !hasAscii && types[i] ; i++) 
        if (!strcmp(types[i], NXAsciiPboardType)) hasAscii=1;
    if (hasAscii) {
		[pasteboard readType:NXAsciiPboardType data:&inData
			length:&inLength];
		if (inData && inLength) {
			if (strcmp(language,"C")==0) {
				int i;
				outLength=inLength+6;
				outData=malloc(outLength);
				outData[0]='/';
				outData[1]='*';
				outData[2]='\n';
				for(i=0;i<inLength;i++)
					outData[i+3]=inData[i];
				outData[inLength+3]='*';
				outData[inLength+4]='/';
				outData[inLength+5]='\n';
				[pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
				[pasteboard writeType:NXAsciiPboardType data:outData
					length:outLength];
				free(outData);
			} else if (strcmp(language,"ObjC")==0) {
				int i=0, j=2, incLength;
				outLength=inLength+1000;
				outData=malloc(outLength);
				outData[0]='/';
				outData[1]='/';
				incLength=2;
				while (i<inLength) {
					if (inData[i] != '\n' || i == inLength-1) {
						outData[j]=inData[i];
						i++;
						j++;
					} else {
						outData[j]=inData[i];
						outData[j+1]='/';
						outData[j+2]='/';
						i++;
						j+=3;
						incLength+=2;
					}
				}
				[pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
				[pasteboard writeType:NXAsciiPboardType data:outData
					length:inLength+incLength];
				free(outData);
			} else if (strcmp(language,"Postscript")==0) {
				int i=0, j=1, incLength;
				outLength=inLength+1000;
				outData=malloc(outLength);
				outData[0]='%';
				incLength=1;
				while (i<inLength) {
					if (inData[i] != '\n' || i == inLength-1) {
						outData[j]=inData[i];
						i++;
						j++;
					} else {
						outData[j]=inData[i];
						outData[j+1]='%';
						i++;
						j+=2;
						incLength+=1;
					}
				}
				[pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
				[pasteboard writeType:NXAsciiPboardType data:outData
					length:inLength+incLength];
				free(outData);
			} else if (strcmp(language,"Sybase")==0) {
				int i=0, j=2, incLength;
				outLength=inLength+1000;
				outData=malloc(outLength);
				outData[0]='-';
				outData[1]='-';
				incLength=2;
				while (i<inLength) {
					if (inData[i] != '\n' || i == inLength-1) {
						outData[j]=inData[i];
						i++;
						j++;
					} else {
						outData[j]=inData[i];
						outData[j+1]='-';
						outData[j+2]='-';
						i++;
						j+=3;
						incLength+=2;
					}
				}
				[pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
				[pasteboard writeType:NXAsciiPboardType data:outData
					length:inLength+incLength];
				free(outData);
			} else if (strcmp(language,"Debug")==0) {
				int i;
				outLength=inLength+20;
				outData=malloc(outLength);
				strcpy(outData,"#ifdef DEBUG\n");
				for(i=0;i<inLength;i++)
					outData[i+13]=inData[i];
				if (inData[inLength-1]!='\n')
					strcpy(&(outData[inLength+13]),"\n#endif");
				else
					strcpy(&(outData[inLength+13]),"#endif\n");
				[pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
				[pasteboard writeType:NXAsciiPboardType data:outData
					length:outLength];
				free(outData);
			} else if (strcmp(language,"Shell")==0) {
				int i=0, j=1, incLength;
				outLength=inLength+1000;
				outData=malloc(outLength);
				outData[0]='#';
				incLength=1;
				while (i<inLength) {
					if (inData[i] != '\n' || i == inLength-1) {
						outData[j]=inData[i];
						i++;
						j++;
					} else {
						outData[j]=inData[i];
						outData[j+1]='#';
						i++;
						j+=2;
						incLength+=1;
					}
				}
				[pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
				[pasteboard writeType:NXAsciiPboardType data:outData
					length:inLength+incLength];
				free(outData);
			}
		}
	}
	return self;
}

- unComment:(id)pasteboard
	userData:(const char *)language
	error:(char **)errorMessage
{
	BOOL inString;
	const char *inData;
	char *outData; 
	int   inLength, outLength, currLength;
    const char *const *types;
    int hasAscii, i;
    types = [pasteboard types];
    hasAscii=0;
	outLength=0;
	currLength=0;
	inString=NO;
    for (i=0; !hasAscii && types[i] ; i++) 
        if (!strcmp(types[i], NXAsciiPboardType)) hasAscii=1;
    if (hasAscii) {
		[pasteboard readType:NXAsciiPboardType data:&inData
			length:&inLength];
		if (inData && inLength) {
			outData=malloc(inLength);
			while (currLength < inLength) {
				if (!inString && inData[currLength]=='/') {
					if (inData[currLength+1]=='*') {
						currLength+=2;
						if (inData[currLength]=='\n' &&
								currLength<inLength)
							currLength++;
						while (!(inData[currLength]=='*' &&
								inData[currLength+1]=='/') &&
								currLength < inLength)
							outData[outLength++]=inData[currLength++];
						currLength+=2;
						if (inData[currLength]=='\n' &&
								currLength<inLength) {
							currLength++;
						} else if (currLength > inLength) {
							*errorMessage="End of C comment not found.";
							return self;
						}
					} else if (inData[currLength+1]=='/') {
						currLength+=2;
						while (inData[currLength]!='\n' &&
								currLength<inLength)
							outData[outLength++]=inData[currLength++];
					} else {
						outData[outLength++]=inData[currLength++];
					}
				} else if (!inString && inData[currLength]=='%') {
						currLength+=1;
						while (inData[currLength]!='\n' &&
								currLength<inLength)
							outData[outLength++]=inData[currLength++];
				} else if (!inString && inData[currLength]=='-'
					&& inData[currLength+1]=='-') {
						currLength+=2;
						while (inData[currLength]!='\n' &&
								currLength<inLength)
							outData[outLength++]=inData[currLength++];
				} else if (!inString && inData[currLength]=='#') {
					if (strncmp(&(inData[currLength+1]),"ifdef DEBUG",11) == 0) {
						currLength+=12;
						if (inData[currLength]=='\n' &&
								currLength<inLength)
							currLength++;
						while ((strncmp(&(inData[currLength]),"\n#endif",7) != 0) &&
							currLength < inLength)
							outData[outLength++]=inData[currLength++];
						currLength+=7;
						if (currLength > inLength) {
							*errorMessage="End of DEBUG statement not found.";
							return self;
						}
					} else {
						currLength+=1;
						while (inData[currLength]!='\n' &&
								currLength<inLength)
							outData[outLength++]=inData[currLength++];
					}
				} else {
					outData[outLength++]=inData[currLength++];
				}
			}
			[pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
			[pasteboard writeType:NXAsciiPboardType data:outData
				length:outLength];
			free(outData);
		}
	}
	return self;
}

- showInfoPanel:sender
{
	if (!infoPanel)
		[NXApp loadNibSection:"InfoPanel.nib" owner:self];
	[infoPanel makeKeyAndOrderFront:self];
	return self;
}
@end
